<?php
// +----------------------------------------------------------------------
// | zhanshop-marketing / Action.php    [ 2024/12/23 15:15 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2024 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
namespace app\api\admin\v1\controller;

use app\api\admin\Controller;
use app\api\admin\v1\service\ActionService;
use zhanshop\App;
use zhanshop\util\Limit;
use zhanshop\server\Request;
use zhanshop\server\Response;

class Action extends Controller
{

    /**
     * 获取设备信息
     * @param Request $request
     * @param array $device
     * @return \zhanshop\Validate|null
     */
    private function getDevice(Request $request, array $device)
    {
        $device = $request->validateData($device, [
            'app' => 'required | string | length:2:32',
            'channel' => 'string',
            'version' => 'string',
            'serial' => 'string',
            'guid' => 'string',
            'ipv4' => 'required | string | length:6:32',
            'ipv6' => 'string',
            'imei' => 'string',
            'oaid' => 'string',
            'android_id' => 'string',
            'idfa' => 'string',
            'iddi' => 'string',
            'mac' => 'string',
            'imsi' => 'string',
            'ua' => 'string',
            'brand' => 'string',
            'unit_type' => 'string',
            'os' => 'int',
            'osver' => 'string',
        ])->getData();
        $device["brand"] = strtolower($device["brand"]);
        $device["channel"] = strtolower($device["channel"]);
        return $device;
    }

    /**
     * @api POST start 启动事件
     * @apiParam json device 设备信息
     * @apiParam string device.app 应用名称
     * @apiParam string device.channel 安装渠道
     * @apiParam string device.version 应用版本
     * @apiParam string device.serial= 安装序号
     * @apiParam string device.guid= 启动标识
     * @apiParam string device.ipv4 客户端IPV4地址
     * @apiParam string device.ipv6 客户端IPV6地址
     * @apiParam string device.imei= 安卓imei
     * @apiParam string device.oaid= 安卓oaid
     * @apiParam string device.android_id= 安卓ID
     * @apiParam string device.idfa= 苹果idfa
     * @apiParam string device.iddi= 注入ID
     * @apiParam string device.mac= 硬件mac地址
     * @apiParam string device.imsi= 国际移动用户识别码
     * @apiParam string device.ua= UA信息
     * @apiParam string device.brand= 设备品牌
     * @apiParam string device.unit_type= 设备型号
     * @apiParam int device.os 系统类型
     * @apiParam string device.osver 系统版本
     * @apiParam string page 访问的页面路径
     * @apiParam string param 访问时页面参数
     * @apiParam string user_id 访问时用户id
     * @apiMiddleware \zhanshop\middleware\SignAuth::class
     * @apiGroup 事件模块
     * @apiDescription 注意：
     */
    public function start(Request $request, Response $response)
    {
        $data = $request->validateRule([
            'device' => 'array',
            'page' => 'string',
            'param' => 'string',
            'user_id' => 'string',
        ])->getData();

        $device = $this->getDevice($request,$data['device']);

        if($device['ipv4'] && filter_var($device['ipv4'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) == false){
            App::error()->setError($device['ipv4'].'是一个无效的IPV4地址', 417);
        }
        if($device['ipv6'] && filter_var($device['ipv6'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) == false){
            App::error()->setError($device['ipv6'].'是一个无效的IPV6地址', 417);
        }

        $realIp = $request->realIp();
        if(filter_var($realIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && $device['ipv4'] == false){
            $device['ipv4'] = $realIp;
        }

        if(filter_var($realIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && $device['ipv6'] == false){
            $device['ipv6'] = $realIp;
        }
        $deviceKey = $device;
        unset($deviceKey['serial'], $deviceKey['guid']);
        $startKey = sha1(json_encode($deviceKey));

        // 启动接口限制3秒冷却期
        if(App::make(Limit::class)->isPass($startKey, 3, 1)){
            App::make(ActionService::class)->start($device, $data['page'], $data['param'], $data['user_id']); // 去启动
        }

        return $this->result();
    }

    /**
     * @api POST startPv 开始访问
     * @apiParam json device 设备信息
     * @apiParam string device.app 应用名称
     * @apiParam string device.channel= 安装渠道
     * @apiParam string device.serial= 安装序号
     * @apiParam string device.ipv4= 客户端IPV4地址
     * @apiParam string device.ipv6= 客户端IPV6地址
     * @apiParam string device.imei= 安卓imei
     * @apiParam string device.oaid= 安卓oaid
     * @apiParam string device.android_id= 安卓ID
     * @apiParam string device.idfa= 苹果idfa
     * @apiParam string device.iddi= 注入ID
     * @apiParam string device.mac= 硬件mac地址
     * @apiParam string device.imsi= 国际移动用户识别码
     * @apiParam string device.ua= UA信息
     * @apiParam string device.brand= 设备品牌
     * @apiParam string device.unit_type= 设备型号
     * @apiParam int device.os 系统类型
     * @apiParam string device.osver 系统版本
     * @apiParam string page 访问的页面路径
     * @apiParam string param 访问时页面参数
     * @apiParam int is_start 是否启动页
     * @apiParam string user_id 访问时用户id
     * @apiMiddleware \zhanshop\middleware\SignAuth::class
     * @apiGroup 事件模块
     * @apiDescription 注意：
     */
    public function startPv(Request $request, Response $response)
    {

        $data = $request->validateRule([
            'device' => 'array',
            'page' => 'string',
            'param' => 'string',
            'is_start' => 'int',
            'user_id' => 'string',
        ])->getData();

        $device = $this->getDevice($request, $data['device']);
        App::make(ActionService::class)->startPv($device, $data['page'], $data['param'], $data['is_start'], $data['user_id']);
        return $this->result();
    }

    /**
     * @api POST endPv 结束访问
     * @apiParam json device 设备信息
     * @apiParam string device.app 应用名称
     * @apiParam string device.channel= 安装渠道
     * @apiParam string device.serial= 安装序号
     * @apiParam string device.ipv4= 客户端IPV4地址
     * @apiParam string device.ipv6= 客户端IPV6地址
     * @apiParam string device.imei= 安卓imei
     * @apiParam string device.oaid= 安卓oaid
     * @apiParam string device.android_id= 安卓ID
     * @apiParam string device.idfa= 苹果idfa
     * @apiParam string device.iddi= 注入ID
     * @apiParam string device.mac= 硬件mac地址
     * @apiParam string device.imsi= 国际移动用户识别码
     * @apiParam string device.ua= UA信息
     * @apiParam string device.brand= 设备品牌
     * @apiParam string device.unit_type= 设备型号
     * @apiParam int device.os 系统类型
     * @apiParam string device.osver 系统版本
     * @apiParam string page 访问的页面路径
     * @apiParam string param 访问时页面参数
     * @apiParam int is_exit 是否退出页
     * @apiParam string user_id 访问时用户id
     * @apiMiddleware \zhanshop\middleware\SignAuth::class
     * @apiGroup 事件模块
     * @apiDescription 注意：
     */
    public function endPv(Request $request, Response $response)
    {
        $data = $request->validateRule([
            'device' => 'array',
            'page' => 'string',
            'param' => 'string',
            'is_exit' => 'int',
            'user_id' => 'string',
        ])->getData();

        $device = $this->getDevice($request, $data['device']);
        App::make(ActionService::class)->endPv($device, $data['page'], $data['param'], $data['is_exit'], $data['user_id']);
        return $this->result();
    }

    /**
     * @api POST register 注册事件
     * @apiParam json device 设备信息
     * @apiParam string device.app 应用名称
     * @apiParam string device.channel= 安装渠道
     * @apiParam string device.serial= 安装序号
     * @apiParam string device.ipv4= 客户端IPV4地址
     * @apiParam string device.ipv6= 客户端IPV6地址
     * @apiParam string device.imei= 安卓imei
     * @apiParam string device.oaid= 安卓oaid
     * @apiParam string device.android_id= 安卓ID
     * @apiParam string device.idfa= 苹果idfa
     * @apiParam string device.iddi= 注入ID
     * @apiParam string device.mac= 硬件mac地址
     * @apiParam string device.imsi= 国际移动用户识别码
     * @apiParam string device.ua= UA信息
     * @apiParam string device.brand= 设备品牌
     * @apiParam string device.unit_type= 设备型号
     * @apiParam int device.os 系统类型
     * @apiParam string device.osver 系统版本
     * @apiParam string user_id 注册时的用户id
     * @apiMiddleware \zhanshop\middleware\SignAuth::class
     * @apiGroup 事件模块
     * @apiDescription 注意：
     */
    public function register(Request $request, Response $response)
    {
        $data = $request->validateRule([
            'device' => 'array',
            'user_id' => 'string',
        ])->getData();

        $device = $this->getDevice($request, $data['device']);

        $result = App::make(ActionService::class)->register($device, $data['user_id']);
        return $this->result($result);
    }

    /**
     * @api POST order 下单事件
     * @apiParam json device 设备信息
     * @apiParam string device.app 应用名称
     * @apiParam string device.channel= 安装渠道
     * @apiParam string device.serial= 安装序号
     * @apiParam string device.ipv4= 客户端IPV4地址
     * @apiParam string device.ipv6= 客户端IPV6地址
     * @apiParam string device.imei= 安卓imei
     * @apiParam string device.oaid= 安卓oaid
     * @apiParam string device.android_id= 安卓ID
     * @apiParam string device.idfa= 苹果idfa
     * @apiParam string device.iddi= 注入ID
     * @apiParam string device.mac= 硬件mac地址
     * @apiParam string device.imsi= 国际移动用户识别码
     * @apiParam string device.ua= UA信息
     * @apiParam string device.brand= 设备品牌
     * @apiParam string device.unit_type= 设备型号
     * @apiParam int device.os 系统类型
     * @apiParam json order 订单信息
     * @apiParam string order.order_id 订单ID
     * @apiParam string order.amount 订单金额
     * @apiParam string order.pay_way 付款方式
     * @apiParam string order.user_id 用户ID
     * @apiMiddleware \zhanshop\middleware\SignAuth::class
     * @apiGroup 事件模块
     * @apiDescription 注意：
     */
    public function order(Request $request, Response $response)
    {
        $data = $request->validateRule([
            'device' => 'array',
            'order' => 'array',
        ])->getData();


        $device = $this->getDevice($request, $data['device']);

        $order = $request->validateData($data['order'], [
            'user_id' => 'string',
            'order_id' => 'string',
            'pay_way' => 'string',
            'amount' => 'string',
        ])->getData();

        App::make(ActionService::class)->order($device, $order['user_id'], $order['order_id'], $order['pay_way'], $order['amount']);
        return $this->result();

    }

    /**
     * @api POST pay 支付事件
     * @apiParam json device 设备信息
     * @apiParam string device.app 应用名称
     * @apiParam string device.channel= 安装渠道
     * @apiParam string device.serial= 安装序号
     * @apiParam string device.ipv4= 客户端IPV4地址
     * @apiParam string device.ipv6= 客户端IPV6地址
     * @apiParam string device.imei= 安卓imei
     * @apiParam string device.oaid= 安卓oaid
     * @apiParam string device.android_id= 安卓ID
     * @apiParam string device.idfa= 苹果idfa
     * @apiParam string device.iddi= 注入ID
     * @apiParam string device.mac= 硬件mac地址
     * @apiParam string device.imsi= 国际移动用户识别码
     * @apiParam string device.ua= UA信息
     * @apiParam string device.brand= 设备品牌
     * @apiParam string device.unit_type= 设备型号
     * @apiParam int device.os 系统类型
     * @apiParam json order 订单信息
     * @apiParam string order.order_id 订单ID
     * @apiParam string order.amount 订单金额
     * @apiParam string order.goods 订单商品
     * @apiMiddleware \zhanshop\middleware\SignAuth::class
     * @apiGroup 事件模块
     * @apiDescription 注意：
     */
    public function pay(Request $request, Response $response)
    {

        $data = $request->validateRule([
            'device' => 'array',
            'order' => 'array',
        ])->getData();

        $order = $request->validateData($data['order'], [
            'user_id' => 'string',
            'order_id' => 'string',
            'pay_way' => 'string',
            'amount' => 'string',
        ])->getData();

        if($data['device']){
            $device = $this->getDevice($request, $data['device']);
        }else{
            $uuid = App::database()->model("visit_register")->where(['user_id' => $order['user_id']])->value("uuid");
            if($uuid == false){
                App::error()->setError("user_id:".$order['user_id'].'没有找到注册记录', 417);
            }
            $device = App::database()->model("visit_device")->where(['uuid' => $uuid])->find();
            if($device == false) App::error()->setError("user_id:".$order['user_id'].',uuid:'.$uuid.'没有找到设备记录', 417);
        }
        App::make(ActionService::class)->pay($device, $order['user_id'], $order['order_id'], $order['pay_way'], $order['amount']);
        return $this->result();
    }

    /**
     * @api POST form 表单事件
     * @apiParam json device 设备信息
     * @apiParam string device.app 应用名称
     * @apiParam string device.channel= 安装渠道
     * @apiParam string device.serial= 安装序号
     * @apiParam string device.ipv4= 客户端IPV4地址
     * @apiParam string device.ipv6= 客户端IPV6地址
     * @apiParam string device.imei= 安卓imei
     * @apiParam string device.oaid= 安卓oaid
     * @apiParam string device.android_id= 安卓ID
     * @apiParam string device.idfa= 苹果idfa
     * @apiParam string device.iddi= 注入ID
     * @apiParam string device.mac= 硬件mac地址
     * @apiParam string device.imsi= 国际移动用户识别码
     * @apiParam string device.ua= UA信息
     * @apiParam string device.brand= 设备品牌
     * @apiParam string device.unit_type= 设备型号
     * @apiParam int device.os 系统类型
     * @apiParam json form 表单内容
     * @apiMiddleware \zhanshop\middleware\SignAuth::class
     * @apiGroup 事件模块
     * @apiDescription 注意：
     */
    public function form(Request $request, Response $response)
    {

    }
    /**
     * @api POST share 分享事件
     * @apiParam json device 设备信息
     * @apiParam string device.app 应用名称
     * @apiParam string device.channel= 安装渠道
     * @apiParam string device.serial= 安装序号
     * @apiParam string device.ipv4= 客户端IPV4地址
     * @apiParam string device.ipv6= 客户端IPV6地址
     * @apiParam string device.imei= 安卓imei
     * @apiParam string device.oaid= 安卓oaid
     * @apiParam string device.android_id= 安卓ID
     * @apiParam string device.idfa= 苹果idfa
     * @apiParam string device.iddi= 注入ID
     * @apiParam string device.mac= 硬件mac地址
     * @apiParam string device.imsi= 国际移动用户识别码
     * @apiParam string device.ua= UA信息
     * @apiParam string device.brand= 设备品牌
     * @apiParam string device.unit_type= 设备型号
     * @apiParam int device.os 系统类型
     * @apiParam json share 分享内容
     * @apiMiddleware \zhanshop\middleware\SignAuth::class
     * @apiGroup 事件模块
     * @apiDescription 注意：
     */
    public function share(Request $request, Response $response)
    {

    }

    /**
     * @api POST custom 自定义事件
     * @apiParam json device 设备信息
     * @apiParam string device.app 应用名称
     * @apiParam string device.channel 安装渠道
     * @apiParam string device.serial 安装序号
     * @apiParam string device.ipv4= 客户端IPV4地址
     * @apiParam string device.ipv6= 客户端IPV6地址
     * @apiParam string device.imei= 安卓imei
     * @apiParam string device.oaid= 安卓oaid
     * @apiParam string device.android_id= 安卓ID
     * @apiParam string device.idfa= 苹果idfa
     * @apiParam string device.iddi= 注入ID
     * @apiParam string device.mac= 硬件mac地址
     * @apiParam string device.imsi= 国际移动用户识别码
     * @apiParam string device.ua= UA信息
     * @apiParam string device.brand= 设备品牌
     * @apiParam string device.unit_type= 设备型号
     * @apiParam int device.os 系统类型
     * @apiParam string event_name 事件名
     * @apiParam json event_param 事件参数
     * @apiMiddleware \zhanshop\middleware\SignAuth::class
     * @apiGroup 事件模块
     * @apiDescription 注意：
     */
    protected function custom(Request $request, Response $response)
    {

    }
}